package com.gs.synedocket.activity.base;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.gs.synedocket.activity.LoginActivity;
import com.gs.synedocket.activity.MainActivity;
import com.gs.synedocket.constant.AllConstants;
import com.gs.synedocket.constant.HandleCode;
import com.gs.synedocket.db.dao.LogUtil;
import com.gs.synedocket.db.sqlite.DBApi;
import com.gs.synedocket.db.sqlite.DBHelper;
import com.gs.synedocket.dialog.WarningDialog;
import com.gs.synedocket.util.ActivityUtil;
import com.gs.synedocket.util.SPUtil;

import java.lang.ref.WeakReference;

public class BaseTouchActivity extends FragmentActivity {
    private WeakReference<FragmentActivity> weakReference = null;
    private String mActivityName, mLoginName;
    private final Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == HandleCode.MSG_COUNTDOWN) {
                if (!ActivityUtil.getInstance().isTopActivity()) {
                    Intent intent1 = new Intent();
                    intent1.setClass(BaseTouchActivity.this, MainActivity.class);
                    intent1.putExtra(AllConstants.WHAT, HandleCode.MSG_WARNING);
                    startActivity(intent1);
                    return;
                }
                showWarning();
            }
        }
    };

    protected void showWarning() {
        WarningDialog warningDialog = new WarningDialog(BaseTouchActivity.this, countDown, () -> {
            handler.removeCallbacks(runnable);
            LogUtil.getInstance().insertOrReplaceData("超時登出");
            DBApi.getInstance().deleteUserTruckTable();
            ActivityUtil.getInstance().finishActivity(BaseTouchActivity.this);
        });
        warningDialog.setOnDismissListener(dialog -> {
            warningDialog.cancelCountDown();
            if (!TextUtils.isEmpty(SPUtil.getInstance().getString(AllConstants.TOKEN))) {
                startTiming();
            }
        });
        if (!BaseTouchActivity.this.isFinishing()) {
            warningDialog.show();
        }
    }

    private long time = 3 * 60 * 60 * 1000;
    private long countDown = 60 * 1000;

//    private long time = 60 * 1000; // 测试用
//    private long countDown = 10 * 1000;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        if (weakReference == null) {
            weakReference = new WeakReference<>(this);
        }
        mActivityName = getClass().getSimpleName();
        mLoginName = LoginActivity.class.getSimpleName();
        ActivityUtil.getInstance().addActivity(weakReference.get());
        if (mActivityName.equals(mLoginName)) {
            return;
        }
        startTiming();
    }

    private void startTiming() {
        handler.removeCallbacks(runnable);
        handler.postDelayed(runnable, time - countDown);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (mActivityName.equals(mLoginName)) {
            return super.dispatchTouchEvent(ev);
        }
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                handler.removeCallbacks(runnable);
                break;
            case MotionEvent.ACTION_UP:
                handler.removeCallbacks(runnable);
                handler.postDelayed(runnable, time - countDown);
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    private final Runnable runnable = () -> handler.sendEmptyMessage(HandleCode.MSG_COUNTDOWN);

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityUtil.getInstance().removeActivity(weakReference.get());
        ActivityUtil.getInstance().removeFragment(this);
    }
}
